--
-- Author: shunguo.chen
-- Date: 2019-05-24 14:25:47
--
-- SkillRunePanel 技能符文panel
--

local ListViewVertical = require("app.widget.listview.ListViewVertical")

local RuneTipsPanel = import(".RuneTipsPanel")
local RuneIconRender = import(".renders.RuneIconRender")
local RuneListRender = import(".renders.RuneListRender")

local ClassRef = app.mvc.createPanelClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node)
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    -- left skills
    local leftPanel = self._ui.Panel_left
    self._specNameText = leftPanel:getChildByName("title")
    self._specTipText = leftPanel:getChildByName("tips")

    local iconRenders = {}
    local render
    for i=1,Constant.Rune_EquipPlace_Num do
        render = RuneIconRender.create(leftPanel:getChildByName("icon" .. i))
        render:onBtnClick(handler(self, self._setCurRuneRender))
        iconRenders[i] = render
    end
    self._iconRenders = iconRenders

    -- right option runes
    local rightPanel = self._ui.Panel_list
    self._runeNameText = rightPanel:getChildByName("title")
    self._runeTipText = rightPanel:getChildByName("tips")

    local params = {
        renderClass = RuneListRender,
        perNum = 1,
        cellEventProxy = handler(self, self._onRuneListEvent),
        scrollEventListen = function() self:_hideTips() end
    }
    self._runeSV = ListViewVertical.new(rightPanel:getChildByName("ScrollView"), params)

    self._worldPos = self:getParent():convertToWorldSpace(cc.p(0,0))
end

-- 镶嵌/卸下符文
function ClassRef:updateRune(rune)
    for i,render in ipairs(self._iconRenders) do
        local data = render:getData()
        if data and data:getEquipPlace() == rune:getEquipPlace() then
            render:refresh()
            self:_setCurRuneRender(render)
            break
        end
    end
end

-- specType SpecType 专精
-- runesList {{Rune, ...}, ...} 按位置分类的符文列表
function ClassRef:refresh(specType, runesList)
    self:_hideTips()

    self._specType = specType
    self._runesList = runesList

    for i,render in ipairs(self._iconRenders) do
        render:setData(runesList[i][1])
    end

    self:_refreshSpecType()
    self:_setCurRuneRender(self._iconRenders[1])
end

function ClassRef:_refreshSpecType()
    local specType = self._specType

    self._specNameText:setString(specType:getNameDes())
    self._specTipText:setString(specType:isActive() and L("spec_state_1") or L("spec_state_2"))
end

function ClassRef:_setCurRuneRender(render)
    if self._curRuneRender then
        self._curRuneRender:setSelected(false)
    end
    render:setSelected(true)
    self._curRuneRender = render

    self._curRune = render:getData()
    self:refreshRuneList()
end

function ClassRef:_onRuneListEvent(eventName, render)
    local rune = render:getData()
    if eventName == "rune_equip" then
        executeFunc(self._onOperate, "rune_equip", rune)
        self:_playAction(render)
    elseif eventName == "rune_unequip" then
        executeFunc(self._onOperate, "rune_unequip", rune)
    elseif eventName == "rune_select" then
        if render ~= self._curListRender then
            self._curListRender = render
            self:_showTips()
        end
    end
end

function ClassRef:refreshRuneList()
    local equipPlace = self._curRune:getEquipPlace()
    local runes = self._runesList[equipPlace] or {}
    self._runeSV:reloadData(runes)

    local gotNum = 0
    for i,rune in ipairs(runes) do
        if rune:isGot() then
            gotNum = gotNum + 1
        end
    end

    local numStr = table.concat( { L("lua_code_text_289"), gotNum, "/", #runes } )
    self._runeTipText:setString(numStr)
    self._runeNameText:setString(self._curRune:getSkillName())
end

function ClassRef:_playAction(render)
    local actionNode = self._actionNode
    local actionTimeline = self._actionTimeline
    if not actionNode then
        actionNode = cc.CSLoader:createNode(Res.upgradeNode,false,false)
        actionTimeline = actionNode:getActionByTag(0)
        actionTimeline:setLastFrameCallFunc(function()
            actionNode:setVisible(false)
        end)
        self:addChild(actionNode)
    end

    local worldPos = render:getIconWorldPos()
    local localPos = self:convertToNodeSpace(worldPos)
    actionNode:setPosition(localPos.x, localPos.y)
    actionTimeline:gotoFrameAndPlay(0, false)
end

function ClassRef:_hideTips()
    if nil == self._curListRender then return end

    if self._curListRender then
        self._curListRender:setIconSelected(false)
        self._curListRender = nil
    end
    if self._runeTip then
        self._runeTip:setVisible(false)
    end
    if self._runeTipBg then
        self._runeTipBg:setVisible(false)
    end
end

function ClassRef:_showTips()
    local listRender = self._curListRender
    local rune = listRender:getData()
    listRender:setIconSelected(true)
    local renderBox = listRender:getBoundingBox()
    local worldPos = listRender:getParent():convertToWorldSpace(cc.p(renderBox.x, renderBox.y))
    renderBox.x = worldPos.x
    renderBox.y = worldPos.y

    local runeTip = self._runeTip
    if not runeTip then
        runeTip = RuneTipsPanel.create()
        runeTip:registerCb(function()
            self:_hideTips()
        end)
        self:getParent():addChild(runeTip, 200)
        self._runeTip = runeTip

        -- 点击空白区域关闭
        display.nodeRemoveForNotHit(runeTip:getChildByName("Panel"), function()
            self:_hideTips()
        end)
    end

    runeTip:setData(rune)
    renderBox.x = renderBox.x - self._worldPos.x
    renderBox.y = renderBox.y - self._worldPos.y
    display.adjustNodePos(renderBox, runeTip, 0, 0)

    local runeTipBg = self._runeTipBg
    if not runeTipBg then
        runeTipBg = display.newColorLayer(cc.c4b(0,0,0,255*0.45))
        runeTipBg:setPosition(cc.p(- self._worldPos.x,- self._worldPos.y))
        self:getParent():addChild(runeTipBg, 199)
        self._runeTipBg = runeTipBg

        local runeIconRender = RuneIconRender.create()
        runeIconRender:setType(2)
        runeIconRender:setName("runeIcon")
        runeIconRender:setScale(0.8)
        runeTipBg:addChild(runeIconRender)
        runeIconRender:setSelected(true)
    end
    runeTipBg:setVisible( true )

    local runeIconRender = runeTipBg:getChildByName("runeIcon")
    runeIconRender:setPosition(listRender:getIconWorldPos())
    runeIconRender:setData(rune)
    runeIconRender:setDotVisible(false)
end

return ClassRef
